package com.sali.design_pattern.a_simple_factory_pattern.lesson_03.mode.factory;

import com.sali.design_pattern.a_simple_factory_pattern.lesson_03.mode.product.IUserVipService;
import com.sali.design_pattern.a_simple_factory_pattern.lesson_03.mode.product.NormalUserVip2Service;
import com.sali.design_pattern.a_simple_factory_pattern.lesson_03.mode.product.SvipUserVip2Service;
import com.sali.design_pattern.a_simple_factory_pattern.lesson_03.mode.product.YearUesrVip2Service;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 简单工厂设计模式，解决一类产品对象的管理
 * 注册表优化
 * @date: 2022/11/13 19:28.
 * @version: 1.0
 */
@Component
public class UserVipServiceFactory2 implements ApplicationContextAware {


    private static final Map<Integer, IUserVipService> ioc = new ConcurrentHashMap<>();

    public static IUserVipService getInstance(Integer vip) {
        return ioc.get(vip);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        // 普通用户
        ioc.put(1, applicationContext.getBean(NormalUserVip2Service.class));
        // 年vip
        ioc.put(2, applicationContext.getBean(YearUesrVip2Service.class));
        // svip
        ioc.put(3, applicationContext.getBean(SvipUserVip2Service.class));
    }
}
